<template>
  <div class="container">
    <div class="hd">
      <div class="hd-left">
        <el-input
          size="small"
          clearable
          placeholder="请输入包房名称"
          style="width: 200px"
          v-model="searchName"
        ></el-input>
        <el-select
          clearable
          v-model="searchCanteen"
          class="m-2"
          placeholder="请选择食堂"
          size="small"
          style="width: 200px; margin: 0 10px"
        >
          <el-option
            v-for="item in canteenOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <TsButton
          perms="chartered-room:page:get"
          label="搜索"
          size="small"
          type="success"
          @click="search"
        ></TsButton>
      </div>
      <div class="hd-right">
        <TsButton
          perms="chartered-room:post"
          label="添加"
          size="small"
          type="primary"
          @click="addRoom"
        ></TsButton>
        <TsButton
          perms="chartered-room:delete"
          size="small"
          type="danger"
          @click="delMute"
          label="删除"
        ></TsButton>
      </div>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="messageName"
          label="包房名称"
          width="200"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="locationAreaName" label="食堂区域" width="200">
        </el-table-column>
        <el-table-column
          prop="messageRemark"
          label="包房备注"
          width="240"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <TsButton
              perms="chartered-room:$id:put"
              style="color: #e6a23c"
              type="text"
              size="mini"
              label="修改"
              @click="update(scope.row.messageId)"
            ></TsButton>
            <TsButton
              perms="chartered-room:delete"
              style="color: #f56c6c"
              type="text"
              size="mini"
              label="删除"
              @click="del(scope.row.messageId)"
            ></TsButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
    <!--    添加编辑对话框-->
    <el-dialog
      v-model="dialogVisible"
      :title="roomId ? '编辑包房信息' : '添加包房信息'"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="roomForm" label-width="100px">
        <el-form-item label="包房名称" prop="messageName">
          <el-input
            style="width: 300px"
            class="m-2"
            size="small"
            v-model="roomForm.messageName"
          ></el-input>
        </el-form-item>
        <el-form-item label="食堂区域" prop="messageLocationId">
          <el-select
            style="width: 300px"
            v-model="roomForm.messageLocationId"
            class="m-2"
            size="small"
          >
            <el-option
              v-for="item in canteenOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="包房备注" prop="messageRemark">
          <el-input
            style="width: 300px"
            type="textarea"
            v-model="roomForm.messageRemark"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import {
  canteenList,
  roomList,
  roomData,
  delRoom,
  roomInfo,
  updateRoom,
} from "../../../api/canteen/room";
import commonPageRepository from "@/composables/common-page-repository";
import { ElMessage, ElMessageBox } from "element-plus";

const searchName = ref("");
//搜索食堂数据
const searchCanteen = ref("");
//食堂下拉列表
const canteenOptions = ref([]);
//获取食堂列表数据
function getCanteenList() {
  canteenList().then((res) => {
    canteenOptions.value = res.data;
  });
}
//搜索方法
function search() {
  roomList({
    canteenId: searchCanteen.value,
    messageName: searchName.value,
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//包房表格数据
const tableData = ref([]);
//分页查询数据
const pageNum = ref(1);
const pageSize = ref(50);
const totalNum = ref(0);
//获取包房数据
function getRoomList() {
  roomList({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getRoomList
);
//添加包房数据
function addRoom() {
  dialogVisible.value = true;
}
//包房信息
const roomForm = ref({});
//对话框显示与隐藏
const dialogVisible = ref(false);
//编辑对话框时的回调函数
function handleClose() {
  cancel();
}
//提交对话框信息
function submit() {
  if (!roomId.value) {
    roomData({
      messageLocationId: roomForm.value.messageLocationId,
      messageName: roomForm.value.messageName,
      messageRemark: roomForm.value.messageRemark,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("添加成功");
        dialogVisible.value = false;
        roomId.value = "";
        getRoomList();
        roomForm.value = {};
      }
    });
  } else {
    updateRoom(roomId.value, {
      messageLocationId: roomForm.value.messageLocationId,
      messageName: roomForm.value.messageName,
      messageRemark: roomForm.value.messageRemark,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("更新成功");
        dialogVisible.value = false;
        getRoomList();
        roomForm.value = {};
        roomId.value = "";
      }
    });
  }
}
//取消对话框提交信息
function cancel() {
  dialogVisible.value = false;
  roomForm.value = {};
  roomId.value = "";
  getRoomList();
}
//多选数据
const muteArr = ref([]);
//表格多选方法
function handleSelectionChange(val) {
  muteArr.value = val.map((item) => item.messageId).join(",");
}
//多选删除
function delMute() {
  if (muteArr.value.length === 0) {
    ElMessage.warning("请选择数据后再试");
    return;
  }
  delRoom({
    ids: muteArr.value,
  }).then((res) => {
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getRoomList();
    }
  });
}
//包房ID
const roomId = ref("");
//修改
function update(id) {
  roomId.value = id;
  dialogVisible.value = true;
  roomInfo(id).then((res) => {
    roomForm.value = res.data;
    roomForm.value.messageLocationId = res.data.messageLocationId * 1;
  });
}
//单个删除
function del(id) {
  ElMessageBox.confirm("您确定要删除这条数据么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delRoom({
        ids: id,
      }).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          getRoomList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
onMounted(() => {
  getCanteenList();
  getRoomList();
});
</script>

<style scoped lang="scss">
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
  .hd {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
